class Solution(object):
    def videoStitching(self, clips, time):
        clips.sort(key=lambda x: (x[0], -x[1]))
        start = clips[0][0]
        if start > 0:
            return -1
        count = 1
        end = clips[0][1]
        next_end = end
        for c in clips[1:]:
            if end >= time:
                return count
            if c[0] <= end:
                next_end = max(next_end, c[1])
            else:
                end = next_end
                next_end = max(next_end, c[1])
                count += 1
                if end >= time:
                    return count
                if c[0] > end:
                    return -1

        if next_end > end:
            end = next_end
            count += 1
        if end >= time:
            return count
        else:
            return -1


data = Solution()
clips = [[0, 2], [1, 6], [3, 10]]
time = 10
print(data.videoStitching(clips, time))
